﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[LookupList]
	-- Add the parameters for the stored procedure here
	@Filter nvarchar(100),
	@ENTITYLIST nvarchar(1000) = '''Person'''
AS
BEGIN
CREATE TABLE #TempTable
(
	LOOKUPNAME nvarchar(100),
    ENTITYID nvarchar(100),
	ENTITYNAME nvarchar(100),
	ENTITYTYPE nvarchar(100),
	ENTITYSUBTYPE nvarchar(100),
	ATTRIBUTENAME nvarchar(100),
)
	DECLARE @FETCHINTVIEWS nvarchar(max)
	DECLARE @WHERECLAUSE nvarchar(255)

	IF (@EntityList='*') SET @WHERECLAUSE = 'WHERE (LookupName IS NOT NULL)'
	ELSE SET @WHERECLAUSE = 'WHERE (EntityType IN (' + @EntityList + ')) AND (LookupName IS NOT NULL)'
	SET @FETCHINTVIEWS = 'INSERT INTO #TempTable  
	SELECT LookupName, EntityID, EntityName, EntityType, EntitySubType, ''LookupName'' AS AttributeName
		FROM dbo.Entities ' + @WHERECLAUSE
	EXECUTE(@FETCHINTVIEWS)
	INSERT INTO #TempTable EXEC ExternalEntityLookups @EntityList=@EntityList
-- Later we will include attributes (like PersonID, Email, etc.) that are NOT in LookupName, but could be helpful.
--IF (@PersonAtt=1 AND @PersonID=1)
--	INSERT INTO #TempTable SELECT * FROM Lookup_Persons_AllAttributes
--ELSE
--IF (@PersonAtt=1 AND @PersonID=0)
--	INSERT INTO #TempTable SELECT * FROM Lookup_Persons_AllAttributes_MinusID



SELECT * FROM #TempTable WHERE LOOKUPNAME LIKE '%'+@FILTER+'%'
DROP TABLE #TempTable
END